package com.example.demo.utils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ReadExcelUtil {

    /**
     * 仅支持2003
     *
     * @param file
     * @throws IOException
     */
    private static void readExcel2003(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        Workbook rwb = new HSSFWorkbook(is);
        Sheet sheet = rwb.getSheetAt(0);
        Row row = sheet.getRow(3);
        Cell cell = row.getCell(0);
        System.out.println(cell.getStringCellValue());
    }

    /**
     * 仅支持2007
     *
     * @param file
     * @throws IOException
     */
    private static void readExcel2007(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        Workbook rwb = new XSSFWorkbook(is);
        Sheet sheet = rwb.getSheetAt(0);
        Row row = sheet.getRow(3);
        Cell cell = row.getCell(0);
        System.out.println(cell.getStringCellValue());
    }

    /**
     * 支持2003/2007
     *
     * @param file
     * @throws Exception
     */
    private static void readExcel(File file) throws Exception {
        InputStream is = new FileInputStream(file);
        Workbook rwb = WorkbookFactory.create(is);
        Sheet sheet = rwb.getSheetAt(0);
        Row row = sheet.getRow(3);
        Cell cell = row.getCell(0);
        System.out.println(cell.getStringCellValue());
    }
}
